package com.changcheng.project.yuminsi.service.impl;

import com.alibaba.fastjson.JSON;
import com.changcheng.project.yuminsi.domain.CheckReportDomain;
import com.changcheng.project.yuminsi.domain.OperInfoDomain;
import com.changcheng.project.yuminsi.domain.UpgradeRecordDomain;
import com.changcheng.project.yuminsi.error.BusinessException;
import com.changcheng.project.yuminsi.error.GlobalErrorCode;
import com.changcheng.project.yuminsi.mapper.CommonMapper;
import com.changcheng.project.yuminsi.mapper.NewCommonMapper;
import com.changcheng.project.yuminsi.service.NewEtcAfterSaleService;
import com.changcheng.project.yuminsi.utils.GenerateSignUtil;
import com.changcheng.project.yuminsi.vo.AcctStatusQueryResponse;
import com.changcheng.project.yuminsi.vo.BaseOutput;
import com.changcheng.project.yuminsi.vo.CheckReportRequest;
import com.changcheng.project.yuminsi.vo.UpgradeRecordRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuminsi
 * @description
 * @date 2020/11/10 15:20
 * @since
 */
@SuppressWarnings("all")
@Service
public class NewEtcAferSaleServiceImpl implements NewEtcAfterSaleService {
    private Logger logger = LoggerFactory.getLogger(NewEtcAferSaleServiceImpl.class);

    @Autowired
    private NewCommonMapper newCommonMapper;

    @Autowired
    private CommonMapper commonMapper;

    @Autowired
    private HttpServletRequest request;

    private static String appkey = "9065275281";
    private static String secretkey = "2b673290caa2d9a82f1f7e845c80e4fe";


    /**
     * OBU固件升级记录同步
     * @param upgradeRecordRequest
     * @return
     */
    @Override
    public BaseOutput upgradeRecordSync(List<UpgradeRecordRequest> upgradeRecordRequest) throws Exception {
        //已经登录放行
        String token = request.getHeader("token");
        OperInfoDomain operInfoDomain = commonMapper.queryOperInfoByToken(token);
        if (operInfoDomain == null) {
            throw new BusinessException(GlobalErrorCode.TOKEN_FAIL);
        }

        //拿到请求头数据
        String IMEI = request.getHeader("IMEI");

        //签名
        String sign = request.getHeader("bt-auth-sign");
        //header
        String appkey = request.getHeader("bt-auth-appkey");
        String nonce = request.getHeader("bt-auth-nonce");
        String timestamp = request.getHeader("bt-auth-timestamp");

        //url
        String uri = request.getRequestURI();

        //参数
        //对象转json
        String upgradeRecord = JSON.toJSONString(upgradeRecordRequest);
        Map<String, Object> postBody = new HashMap<>();
        postBody.put("json", upgradeRecord);
        String genSign = GenerateSignUtil.generateSignOfPost(uri, appkey, nonce, timestamp, postBody, secretkey);
        logger.info("POST生成的签名:  {}", genSign);

        if (!genSign.equalsIgnoreCase(sign)) {
            throw new BusinessException(GlobalErrorCode.SIGN_FAIL);
        }

        List<UpgradeRecordDomain> upgradeRecordDomains = new ArrayList<>();
        UpgradeRecordDomain upgradeRecordDomain = null;
        if (!CollectionUtils.isEmpty(upgradeRecordRequest)){
            for (UpgradeRecordRequest upgradeRecordRequest1 : upgradeRecordRequest){
                upgradeRecordDomain = new UpgradeRecordDomain();
                upgradeRecordDomain.setVin(upgradeRecordRequest1.getVin());
                upgradeRecordDomain.setHandsetImei(upgradeRecordRequest1.getHandsetImei());
                upgradeRecordDomain.setObuSn(upgradeRecordRequest1.getObuSn());
                upgradeRecordDomain.setObuBrand(upgradeRecordRequest1.getObuBrand());
                upgradeRecordDomain.setObuMac(upgradeRecordRequest1.getObuMac());
                upgradeRecordDomain.setObuIcNumber(upgradeRecordRequest1.getObuIcNumber());
                upgradeRecordDomain.setOperator(upgradeRecordRequest1.getOperator());
                upgradeRecordDomain.setServiceStationName(upgradeRecordRequest1.getServiceStationName());
                upgradeRecordDomain.setServiceStationNo(upgradeRecordRequest1.getServiceStationNo());
                upgradeRecordDomain.setBeforeUpgradeVersion(upgradeRecordRequest1.getBeforeUpgradeVersion());
                upgradeRecordDomain.setAfterUpgradeVersion(upgradeRecordRequest1.getAfterUpgradeVersion());
                upgradeRecordDomain.setUpgradeResults(upgradeRecordRequest1.getUpgradeResults());
                upgradeRecordDomain.setUpgradeTime(upgradeRecordRequest1.getUpgradeTime());
                upgradeRecordDomains.add(upgradeRecordDomain);
            }
        }

        try {
            newCommonMapper.insertUpgradeRecord(upgradeRecordDomains);
        } catch (Exception e) {
            logger.info("异常,{}",e);
            return BaseOutput.responseData(GlobalErrorCode.FAIL,e);
        }
        return BaseOutput.responseData(GlobalErrorCode.SUCCESS,null);
    }

    /**
     * OBU检测报告同步
     * @param checkReportRequest
     * @return
     */
    @Override
    public BaseOutput checkReportSync(List<CheckReportRequest> checkReportRequest) throws Exception {
        //已经登录放行
        String token = request.getHeader("token");
        OperInfoDomain operInfoDomain = commonMapper.queryOperInfoByToken(token);
        if (operInfoDomain == null) {
            throw new BusinessException(GlobalErrorCode.TOKEN_FAIL);
        }

        //拿到请求头数据
        String IMEI = request.getHeader("IMEI");

        //签名
        String sign = request.getHeader("bt-auth-sign");
        //header
        String appkey = request.getHeader("bt-auth-appkey");
        String nonce = request.getHeader("bt-auth-nonce");
        String timestamp = request.getHeader("bt-auth-timestamp");

        //url
        String uri = request.getRequestURI();

        //参数
        //对象转json
        String checkReport = JSON.toJSONString(checkReportRequest);
        Map<String, Object> postBody = new HashMap<>();
        postBody.put("json", checkReport);
        String genSign = GenerateSignUtil.generateSignOfPost(uri, appkey, nonce, timestamp, postBody, secretkey);
        logger.info("POST生成的签名:  {}", genSign);

        if (!genSign.equalsIgnoreCase(sign)) {
            throw new BusinessException(GlobalErrorCode.SIGN_FAIL);
        }

        List<CheckReportDomain> checkReportDomains = new ArrayList<>();
        CheckReportDomain checkReportDomain;
        if (!CollectionUtils.isEmpty(checkReportRequest)){
            for (CheckReportRequest checkReportRequest1:checkReportRequest){
                checkReportDomain = new CheckReportDomain();
                checkReportDomain.setVin(checkReportRequest1.getVin());
                checkReportDomain.setObuSn(checkReportRequest1.getObuSn());
                checkReportDomain.setOperator(checkReportRequest1.getOperator());
                checkReportDomain.setHandsetImei(checkReportRequest1.getHandsetImei());
                checkReportDomain.setVehicleSourceCode(checkReportRequest1.getVehicleSourceCode());
                checkReportDomain.setBluetoothName(checkReportRequest1.getBluetoothName());
                checkReportDomain.setObuMac(checkReportRequest1.getObuMac());
                checkReportDomain.setServiceStationName(checkReportRequest1.getServiceStationName());
                checkReportDomain.setServiceStationNo(checkReportRequest1.getServiceStationNo());
                checkReportDomain.setObuIssuer(checkReportRequest1.getObuIssuer());
                checkReportDomain.setObuExpireTime(checkReportRequest1.getObuExpireTime());
                checkReportDomain.setObuModel(checkReportRequest1.getObuModel());
                checkReportDomain.setCheckTime(checkReportRequest1.getCheckTime());
                checkReportDomain.setObuIcType(checkReportRequest1.getObuIcType());
                checkReportDomain.setObuIcEnableTime(checkReportRequest1.getObuIcEnableTime());
                checkReportDomain.setBluetoothMac(checkReportRequest1.getBluetoothMac());
                checkReportDomain.setObuDisassemblyStatus(checkReportRequest1.getObuDisassemblyStatus());
                checkReportDomain.setObuContractVersion(checkReportRequest1.getObuContractVersion());
                checkReportDomain.setObuEnableTime(checkReportRequest1.getObuEnableTime());
                checkReportDomain.setObuLicensePlateNum(checkReportRequest1.getObuLicensePlateNum());
                checkReportDomain.setObuLicensePlateColor(checkReportRequest1.getObuLicensePlateColor());
                checkReportDomain.setObuIcUserType(checkReportRequest1.getObuIcUserType());
                checkReportDomain.setObuIcNumber(checkReportRequest1.getObuIcNumber());
                checkReportDomain.setObuIcExpireTime(checkReportRequest1.getObuIcExpireTime());
                checkReportDomain.setCheckResult(checkReportRequest1.getCheckResult());
                checkReportDomain.setZeroConsumptionResult(checkReportRequest1.getZeroConsumptionResult());
                checkReportDomain.setBluetoothCheckResult(checkReportRequest1.getBluetoothCheckResult());
                checkReportDomain.setCheckConclusion(checkReportRequest1.getCheckConclusion());
                checkReportDomains.add(checkReportDomain);
            }
        }

        try {
            newCommonMapper.insertCheckReport(checkReportDomains);
        } catch (Exception e) {
            logger.info("异常,{}",e);
            return BaseOutput.responseData(GlobalErrorCode.FAIL,e);
        }
        return BaseOutput.responseData(GlobalErrorCode.SUCCESS,null);
    }
}
